<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getNewsDetail } from '@/api/news'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const news = ref(null)
const loading = ref(false)

// 获取新闻详情
const getNews = async () => {
  const id = route.params.id
  if (!id) return
  
  loading.value = true
  try {
    const res = await getNewsDetail(id)
    if (res.code === 200) {
      news.value = res.data
    } else {
      ElMessage.error(res.msg || '获取新闻详情失败')
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '获取新闻详情失败')
  } finally {
    loading.value = false
  }
}

// 返回列表
const goBack = () => {
  router.push('/news')
}

onMounted(() => {
  getNews()
})
</script>

<template>
  <div class="news-detail-container">
    <el-card v-loading="loading">
      <template #header>
        <div class="card-header">
          <el-button :icon="ArrowLeft" @click="goBack">返回</el-button>
        </div>
      </template>
      
      <div v-if="news" class="news-content">
        <h1 class="news-title">{{ news.title }}</h1>
        <div class="news-info">
          <span>发布时间：{{ news.publishTime }}</span>
        </div>
        <div class="news-body">
          {{ news.content }}
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.news-detail-container {
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  align-items: center;
}

.news-content {
  padding: 20px 0;
}

.news-title {
  margin: 0 0 20px;
  font-size: 24px;
  color: #303133;
  text-align: center;
}

.news-info {
  text-align: center;
  color: #909399;
  margin-bottom: 30px;
  font-size: 14px;
}

.news-body {
  line-height: 1.8;
  color: #606266;
  font-size: 16px;
  white-space: pre-wrap;
}
</style> 